package com.cxl.live.system.controller;

import com.cxl.live.base.BaseController;
import com.cxl.live.base.ResultModel;
import com.cxl.live.exception.BusinessException;
import com.cxl.live.system.dto.Register;
import com.cxl.live.system.service.RegisterService;
import com.cxl.live.util.RegexUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: RegisterController
 * @Author: ChenXueLi
 * @Date: 2019-02-05 16:20
 * 你是一段成熟的代码，要学会自己改bug
 */
@Slf4j
@Api(tags = "注册用户")
@RequestMapping("/sys/user")
@RestController
public class RegisterController extends BaseController {

    @Autowired RegisterService registerService;

    @ApiOperation(value = "用户注册", httpMethod = "POST")
    @PostMapping("/register")
    public ResultModel register(Register register) {
//        if(RegexUtil.isNull(register.getAccount())) {
//            throw new BusinessException(-1001, "账号不能为空");
//        }
        if(RegexUtil.isNull(register.getPassword())){
            throw new BusinessException(-1002, "密码不能为空");
        }
        // 如果输入的是邮箱账号，设置进邮箱字段
        if(RegexUtil.isEmail(register.getAccount())) {
            register.setEmail(register.getAccount());
        }
        // 如果输入的是手机号账号，设置进手机号字段
        if(RegexUtil.isPhone(register.getAccount())) {
            register.setPhone(register.getAccount());
        }
        if(RegexUtil.isNull(register.getAccount())) {
            throw new BusinessException(-1001, "账号不能为空");
        }

        registerService.register(this.resultModel, register);
        return resultModel;
    }

    @ApiOperation(value = "激活账号", httpMethod = "GET")
    @GetMapping("/active/{code}/{uuid}")
    public ResultModel active(@PathVariable String code, @PathVariable String uuid) {
        // code是5位数的字符（数字）

        // id是32位的uuid序列
        // 实例化返回对象

        registerService.active(this.resultModel, code, uuid);
        return resultModel;
    }

}
